package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseFragment;
import com.epfresh.supplier.R;
import com.epfresh.supplier.fragment.ApplyOpenFirstFragment;
import com.epfresh.supplier.view.MyFragmentPagerAdapter;
import com.epfresh.supplier.view.NoScrollViewPage;
import java.util.ArrayList;
import java.util.List;

public class ApplyOpeningShopActivity extends BaseActivity {
    private NoScrollViewPage noScrollViewPage;
    private MyFragmentPagerAdapter myFragmentPagerAdapter;
    private ApplyOpenFirstFragment registerFirstFragment;
    private ApplyOpenFirstFragment registerSecondFragment;
    private ImageView ivRegister2, ivRegisterToResult;
    private TextView tvRegister2, tvRegisterFail;
    private RelativeLayout rlRegisterFail;
    private int type;//1审核失败2审核中
    private boolean isAuditFail = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.applyopenshop);
        type = getIntent().getIntExtra("type", -1);
        initView();
        initData();
        setToolbarTitle("申请开店");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        // 用户没有进行有效的设置操作，返回
        if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "取消", Toast.LENGTH_LONG).show();
            return;
        }
//        if (registerFirstFragment != null)
//            registerFirstFragment.setActivityResult(requestCode, resultCode, intent);
        super.onActivityResult(requestCode, resultCode, intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void getIntents() {
        setOnRightVisible(0);
        setRightText("帮助");
        if (type == 1) {//审核失败
            rlRegisterFail.setVisibility(View.VISIBLE);
        } else if (type == 2) {//审核中
//            ivRegister2.setBackgroundResource(R.mipmap.registgreen2);
//            tvRegister2.setTextColor(getResources().getColor(R.color.color55bd7c));
//            ivRegisterToResult.setBackgroundResource(R.mipmap.register_toparror);
            isAuditFail = true;
        }
    }

    @Override
    public void onRightClick() {
        super.onRightClick();

    }


    private void initView() {
//        noScrollViewPage = findViewById(R.id.viewpager);
////        ivRegister2 = findViewById(R.id.iv_register2);
////        tvRegister2 = findViewById(R.id.tv_register2);
////        ivRegisterToResult = findViewById(R.id.iv_register_to_result);
//        tvRegisterFail = findViewById(R.id.tv_register_fail);
//        rlRegisterFail = findViewById(R.id.rl_register_fail);
    }

    private void initData() {
        getIntents();
        initVp();
    }

    private void initVp() {
        myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), getFragmentList());
        noScrollViewPage.setAdapter(myFragmentPagerAdapter);
        noScrollViewPage.setOffscreenPageLimit(2);
        noScrollViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                View view = noScrollViewPage.getChildAt(position);
                if (view == null) {
                    return;
                }
                int height = view.getMeasuredHeight();
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) noScrollViewPage.getLayoutParams();
                layoutParams.height = height;
                noScrollViewPage.setLayoutParams(layoutParams);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private List<BaseFragment> getFragmentList() {
        List<BaseFragment> fragmentList = new ArrayList<>();
        if (!isAuditFail) {
            registerFirstFragment = new ApplyOpenFirstFragment();
          //  registerFirstFragment.setListener(registerNextListener, type);
            fragmentList.add(registerFirstFragment);
        }
        registerSecondFragment = new ApplyOpenFirstFragment();
        fragmentList.add(registerSecondFragment);
        return fragmentList;
    }


    public interface RegisterNextListener {
        void next();
        void error(String error);
    }

}
